package TMS.src;

import java.io.File;
import java.io.Serializable;

public class User implements Serializable {
    private String card_number;
    private String name;
    private String password;
    private String identity;
    //public static List<User> alreadyExisted = new ArrayList<>();
    public User() {
    }

    public User(String card_number, String name, String password, String identity) {
        this.card_number = card_number;
        this.name = name;
        this.password = password;
        this.identity = identity;
    }

    void setName(String name) {
        this.name = name;
    }

    String getName() {
        return this.name;
    }

    void setCard_number(String card_number) {
        this.card_number = card_number;
    }

    String getCard_number() {
        return this.card_number;
    }

    void setPassword(String password) {
        this.password = password;
    }

    String getPassword() {
        return this.password;
    }

    void setIdentity(String identity) {
        this.identity = identity;
    }

    String getIdentity() {
        return this.identity;
    }

    @Override
    public String toString() {
        return "Name: " + this.name + "\n" + "Kakafee number: " + this.card_number + "\n" + "Type: " + this.identity + "";
    }
    /**
     * 删除单个文件
     *
     * @param fileName：要删除的文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                //System.out.println("删除单个文件" + fileName + "成功！");
                return true;
            } else {
                // System.out.println("删除单个文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println("删除单个文件失败：" + fileName + "不存在！");
            return false;
        }
    }

    /**
     * 删除目录及目录下的文件
     *
     * @param dir：要删除的目录的文件路径
     * @return 目录删除成功返回true，否则返回false
     */
    public static boolean deleteDirectory(String dir) {
        // 如果dir不以文件分隔符结尾，自动添加文件分隔符
        if (!dir.endsWith(File.separator))
            dir = dir + File.separator;
        File dirFile = new File(dir);
        // 如果dir对应的文件不存在，或者不是一个目录，则退出
        if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
            //System.out.println("删除目录失败：" + dir + "不存在！");
            return false;
        }
        boolean flag = true;
        // 删除文件夹中的所有文件包括子目录
        File[] files = dirFile.listFiles();
        for (File file : files) {
            // 删除子文件
            if (file.isFile()) {
                flag = deleteFile(file.getAbsolutePath());
                if (!flag)
                    break;
            }
            // 删除子目录
            else if (file.isDirectory()) {
                flag = deleteDirectory(file.getAbsolutePath());
                if (!flag)
                    break;
            }
        }
        if (!flag) {
            //System.out.println("删除目录失败！");
            return false;
        }
        // 删除当前目录
        if (dirFile.delete()) {
            //System.out.println("删除目录" + dir + "成功！");
            return true;
        } else {
            return false;
        }
    }
    String Quit(String[] cmd) {
        if(cmd.length!=1)
            return "Illegal argument count";
        else {
            String fileName = "./data";
            File file = new File(fileName);
            if (!file.exists()) {
                //System.out.println("删除文件失败:" + fileName + "不存在！");
            } else {
                if (file.isFile())
                    deleteFile(fileName);
                else
                    deleteDirectory(fileName);
            }
            return "----- Good Bye! -----";
        }
    }
    String Register(String[] cmd) {
        if (cmd.length != 6) {
            return "Illegal argument count";
        }
        String card_number = cmd[1];
        String name = cmd[2];
        String password = cmd[3];
        String rePassword = cmd[4];
        String identity = cmd[5];
        //boolean card_illegal = !card_number.matches("(000[1-9]|00[1-9]\\d|0[1-9]\\d{2}|[1-3]\\d{3}|4[0-4]\\d{2}|4500) (178[5-9]|179\\d|18[0-7]\\d|188[0-6]) ([1-2]\\d{3}|3000)");
        boolean card_illegal = Gadget.check_user_card_illegal(card_number);//!card_number.matches("^(000[1-9]|00[1-9]\\d|0[1-9]\\d{2}|[1-3]\\d{3}|4[0-4]\\d{2}|4500)(178[5-9]|179\\d|18[0-7]\\d|188[0-6])([1-2]\\d{3}|3000)$");
        boolean card_exist = Const.user_existed.containsKey(card_number);
        boolean name_illegal = Gadget.check_name_illegal(name);//!name.matches("^[A-Za-z][A-Za-z_]{3,15}$");
        boolean password_illegal = Gadget.check_password_illegal(password);//!password.matches("^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[@_%$])[a-zA-Z0-9@_%$]{8,16}$");
                                                        //^(?=.*\d)(?=.*[a-zA-Z])(?=.*[@_%$])[a-zA-Z0-9@_%$]{8,16}$
        boolean password_different = !password.equals(rePassword);
        boolean identity_illegal = !(identity.equals("Administrator") || (identity.equals("Merchant")) || (identity.equals("Customer")));
        if (Const.logged_in) {
            return "Already logged in";
        } else if (card_illegal) {
            return "Illegal Kakafee number";
        } else if (card_exist) {
            return "Kakafee number exists";
        } else if (name_illegal) {
            return "Illegal name";
        } else if (password_illegal) {
            return "Illegal password";
        } else if (password_different) {
            return "Passwords do not match ";
        } else if (identity_illegal) {
            return "Illegal identity";
        } else {
            User user = new User(card_number, name, password, identity);
            Const.user_existed.put(card_number, user);
            return "Register success";
        }
    }

    String Login(String[] cmd) {
        if (cmd.length != 3) {
            return "Illegal argument count";
        }
        String card_number = cmd[1];
        String passwords = cmd[2];
        boolean already_existed = Const.user_existed.containsKey(card_number);
        boolean wrong_password = already_existed ? (!(Const.user_existed.get(card_number).password).equals(passwords)) : true;
        boolean card_illegal = Gadget.check_user_card_illegal(card_number);//!card_number.matches("^([0-3]\\d{3}|4[0-4]\\d{2}|4500)(178[5-9]|179\\d|18[0-7]\\d|188[0-6])([1-2]\\d{3}|3000)$");
        if (Const.logged_in) {
            return "Already logged in";
        } else if (card_illegal) {
            return "Illegal Kakafee number ";
        } else if (!already_existed) {
            return "Kakafee number not exists";
        } else if (wrong_password) {
            return "Wrong password";
        } else {
            Const.logged_in = true;
            Const.current_user = Const.user_existed.get(card_number);
            return "Welcome to TMS";
        }
    }

    String Logout(String[] cmd) {
        if (cmd.length != 1) {
            return "Illegal argument count";
        } else if (!Const.logged_in) {
            return "Please log in first";
        }
        Const.logged_in = false;
        Const.current_user = null;
        return "Bye~";
    }

    String PrintInfo(String[] cmd) {
        if (cmd.length > 2) {
            return "Illegal argument count";
        } else if (!Const.logged_in) {
            return "Please log in first";
        } else if (cmd.length == 1) {
            return Const.current_user.toString();
        } else if (!Const.current_user.identity.equals("Administrator")) {
            return "Permission denied";
        } else {
            String card_number = cmd[1];
            boolean card_illegal = Gadget.check_user_card_illegal(card_number);//!card_number.matches("^([0-3]\\d{3}|4[0-4]\\d{2}|4500)(178[5-9]|179\\d|18[0-7]\\d|188[0-6])([1-2]\\d{3}|3000)$");
            boolean already_existed = Const.user_existed.containsKey(card_number);
            if (card_illegal) {
                return "Illegal Kakafee number";
            } else if (!already_existed) {
                return "Kakafee number not exist ";
            } else {
                return Const.user_existed.get(card_number).toString();
            }
        }
    }
}


